package cn.ac.iscas.chinesesegmentme;
///////////////////////////////////////////////////////////////////////////////
// Copyright (C) 2001 Chieu Hai Leong and Jason Baldridge
//
// This library is free software; you can redistribute it and/or
// modify it under the terms of the GNU Lesser General Public
// License as published by the Free Software Foundation; either
// version 2.1 of the License, or (at your option) any later version.
//
// This library is distributed in the hope that it will be useful,
// but WITHOUT ANY WARRANTY; without even the implied warranty of
// MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
// GNU Lesser General Public License for more details.
//
// You should have received a copy of the GNU Lesser General Public
// License along with this program; if not, write to the Free Software
// Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA  02111-1307, USA.
//////////////////////////////////////////////////////////////////////////////   

import java.io.BufferedOutputStream;
import java.io.File;
import java.io.FileNotFoundException;
import java.io.FileOutputStream;
import java.io.FileReader;
import java.io.PrintStream;
import java.text.DecimalFormat;

import opennlp.maxent.BasicContextGenerator;
import opennlp.maxent.ContextGenerator;
import opennlp.maxent.DataStream;
import opennlp.maxent.PlainTextByLineDataStream;
import opennlp.model.GenericModelReader;
import opennlp.model.MaxentModel;
import opennlp.model.RealValueFileEventStream;
import opennlp.perceptron.PerceptronModel;
import opennlp.perceptron.PerceptronModelReader;

/**
 * Test the model on some input.
 * 
 * @author Jason Baldridge
 * @version $Revision: 1.4 $, $Date: 2008/11/06 20:00:34 $
 */
public class Predict {
	MaxentModel _model;
	ContextGenerator _cg = new BasicContextGenerator();

	public Predict(MaxentModel m) {
		_model = m;
	}

	private void eval(String predicates) {
		eval(predicates, false);
	}

	private void eval(String predicates, boolean real) {
		String[] contexts = predicates.split(" ");
		double[] ocs;
		
		if (!real) {
			ocs = _model.eval(contexts);
		} else {
			float[] values = RealValueFileEventStream.parseContexts(contexts);
			ocs = _model.eval(contexts, values);
		}
		System.out.println(getOutputline(ocs));
	}
	//B[0.0305]  E[0.2242]  S[0.7453]  M[0.0000]
	private static String getOutputline(double[] ocs){
        DecimalFormat df =  new DecimalFormat("0.0000");
        StringBuffer sb = new StringBuffer(ocs.length*2);
        sb.append(df.format(ocs[0]));
        for (int i = 1; i<ocs.length; i++) {
          sb.append(" ").append(df.format(ocs[i]));
        }
        return sb.toString();
	}

	private static void usage() {

	}

	/**
	 * Main method. Call as follows:
	 * <p>
	 * java Predict dataFile (modelFile)
	 * @throws FileNotFoundException 
	 */
	public static void main(String[] args) throws FileNotFoundException {
		
		PrintStream ps = new PrintStream(
		        new BufferedOutputStream(new FileOutputStream(
		        new File("data/predict.txt"))), true);
		System.setOut(ps);    
		
		String dataFileName="", modelFileName="";
		boolean real = false;
		String type = "maxent";
		int ai = 0;
		if (args.length > 0) {
			dataFileName = args[ai++];
			if (args.length > ai) {
				modelFileName = args[ai++];
			} else {
				modelFileName = dataFileName.substring(0,
						dataFileName.lastIndexOf('.'))
						+ "Model.txt";
			}
		} else {
			System.err.println("No Input File!");
			System.exit(0);

		}
		Predict predictor = null;
		try {
			MaxentModel m = new GenericModelReader(new File(modelFileName))
			.getModel();
			predictor = new Predict(m);
		} catch (Exception e) {
			e.printStackTrace();
			System.exit(0);
		}


		try {
			DataStream ds = new PlainTextByLineDataStream(new FileReader(
					new File(dataFileName)));

			while (ds.hasNext()) {
				String s = (String) ds.nextToken();
				predictor.eval(s.substring(0, s.lastIndexOf(' ')), real);
			}
			return;
		} catch (Exception e) {
			System.out.println("Unable to read from specified file: "
					+ modelFileName);
			System.out.println();
			e.printStackTrace();
		}
	}

}
